home *** CD-ROM | disk | FTP | other *** search
/ SGI ONC3 NFS v2 / SGI ONC3 NFS v2.iso / dist6.4 / nfs.idb / var / yp / ypinit.z / ypinit
Text File  |  1997-02-07  |  7KB  |  361 lines

  1. #! /bin/sh
  2. #
  3. # @(#)ypinit.sh    1.3 90/07/23 4.1NFSSRC SMI
  4. #
  5. # ypinit.sh - 
  6. #    set up a populated NIS directory structure on a master or slave server
  7. #
  8.  
  9. # set -xv
  10.  
  11. yproot_dir=/var/yp
  12. XFR=/usr/sbin/ypxfr
  13. hf=/tmp/ypinit.hostlist.$$
  14. YPM=${YPMAKE-$yproot_dir/ypmake}
  15. chkconfig=/sbin/chkconfig
  16.  
  17. masterp=F
  18. slavep=F
  19. host=""
  20. def_dom=""
  21. master=""
  22. got_host_list=F
  23. exit_on_error=F
  24. errors_in_setup=F
  25.  
  26. PATH=$PATH:$yproot_dir
  27. export PATH 
  28.  
  29. trap "rm -f $hf; exit 0" 2 15 
  30.  
  31. usage()
  32. {
  33.     exec 1>&2
  34.     echo 'usage:'
  35.     echo '    ypinit -m'
  36.     echo '    ypinit -s master_server'
  37.     echo ""
  38.     echo "\
  39. where -m is used to build a master NIS server data base, and -s is used for"
  40.     echo "\
  41. a slave data base.  master_server must be an existing reachable NIS server."
  42.     exit 1
  43. }
  44.  
  45. case $# in
  46. 1)    case $1 in
  47.     -m)    masterp=T;;
  48.     *)    usage;;
  49.     esac;;
  50.  
  51. 2)    case $1 in
  52.     -s)    slavep=T; master=$2;;
  53.     *)    usage;;
  54.     esac;;
  55.  
  56. *)    usage;;
  57. esac
  58.  
  59.  
  60. if [ $slavep = T ]
  61. then
  62.     maps=`ypwhich -m | egrep $master$| awk '{ printf("%s ",$1) }' -`
  63.     if [ -z "$maps" ]
  64.     then
  65.         echo "\nCan't enumerate maps from $master. Please check that $master is running." 1>&2
  66.         echo "Please make sure ypbind is running on this host." 1>&2
  67.         exit 1
  68.     fi
  69. fi
  70.  
  71. host=`hostname`
  72.  
  73. if [ $? -ne 0 ]
  74. then 
  75.     echo "Can't get local host's name.  Please check your path." 1>&2
  76.     exit 1
  77. fi
  78.  
  79. if [ -z "$host" ]
  80. then
  81.     echo "The local host's name hasn't been set.  Please set it." 1>&2
  82.     exit 1
  83. fi
  84.  
  85. def_dom=`domainname`
  86.  
  87. if [ $? -ne 0 ]
  88. then 
  89.     echo "Can't get local host's domain name.  Please check your path." 1>&2
  90.     exit 1
  91. fi
  92.  
  93. if [ -z "$def_dom" ]
  94. then
  95.     echo "The local host's domain name hasn't been set.  Please set it." 1>&2
  96.     exit 1
  97. fi
  98.  
  99. domainname $def_dom
  100.  
  101. if [ $? -ne 0 ]
  102. then 
  103.     echo "\
  104. You have to be the superuser to run this.  Please log in as root." 1>&2
  105.     exit 1
  106. fi
  107.  
  108. if [ ! -d $yproot_dir -o -f $yproot_dir ]
  109. then
  110.     echo "\
  111. The directory $yproot_dir doesn't exist.  Restore it from the distribution." 1>&2
  112.     exit 1
  113. fi
  114.  
  115. if [ $slavep = T ]
  116. then
  117.     if [ $host = $master ]
  118.     then
  119.         echo "\
  120. The host specified should be a running master NIS server, not this machine." 1>&2
  121.         exit 1
  122.     fi
  123. fi
  124.  
  125. if [ "$setup" != "yes" ]; then
  126.     echo "Installing the NIS data base will require that you answer a few questions."
  127.     echo "Questions will all be asked at the beginning of the procedure."
  128.     echo ""
  129.     echo "Do you want this procedure to quit on non-fatal errors? [y/n: n]  \c"
  130.     read doexit
  131. else
  132.     doexit=yes
  133. fi
  134.  
  135. case $doexit in
  136. y* | Y*)    exit_on_error=T;;
  137. *)    echo "\
  138. OK, please remember to go back and redo manually whatever fails.  If you"
  139.     echo "\
  140. don't, some part of the system (perhaps the NIS itself) won't work.";;
  141. esac
  142.  
  143. echo ""
  144.  
  145. for dir in $yproot_dir/$def_dom
  146. do
  147.  
  148.     if [ -d $dir ]; then
  149.         if [ "$setup" != "yes" ]; then
  150.             echo "Can we destroy the existing $dir and its contents? [y/n: n]  \c"
  151.             read kill_old_dir
  152.         else 
  153.             kill_old_dir=yes
  154.         fi
  155.  
  156.         case $kill_old_dir in
  157.         y* | Y*)    rm -r -f $dir
  158.  
  159.             if [ $?  -ne 0 ]
  160.             then
  161.             echo "Can't clean up old directory $dir.  Fatal error." 1>&2
  162.                 exit 1
  163.             fi;;
  164.  
  165.         *)    echo "OK, please clean it up by hand and start again.  Bye"
  166.             exit 0;;
  167.         esac
  168.     fi
  169.  
  170.     mkdir $dir
  171.  
  172.     if [ $?  -ne 0 ]
  173.     then
  174.         echo "Can't make new directory $dir.  Fatal error." 1>&2
  175.         exit 1
  176.     fi
  177.  
  178. done
  179.  
  180. if [ $slavep = T ]
  181. then
  182.  
  183.     echo "\
  184. There will be no further questions. The remainder of the procedure should take"
  185.     echo "a few minutes, to copy the data bases from $master."
  186.  
  187.     for dom in  $def_dom
  188.     do
  189.         for map in $maps
  190.         do
  191.             echo "Transferring $map..."
  192.             $XFR -h $master -c -d $dom $map
  193.  
  194.             if [ $?  -ne 0 ]
  195.             then
  196.                 errors_in_setup=T
  197.  
  198.                 if [ $exit_on_error = T ]
  199.                 then
  200.                     exit 1
  201.                 fi
  202.             fi
  203.         done
  204.     done
  205.  
  206.     echo ""
  207.  
  208.     if [ $errors_in_setup = T ]
  209.     then
  210.         echo "${host}'s NIS database has been set up with errors." 1>&2
  211.         echo "Please remember to figure out what went wrong, and fix it." 1>&2
  212.     else
  213.         echo "${host}'s NIS database has been set up without any errors."
  214.         if $chkconfig ypserv; then :
  215.         else
  216.             echo ""
  217.             echo "Start NIS slave daemons during system startup? [y/n: y] \c"
  218.             read startyp
  219.             case $startyp in
  220.             n* | N*)
  221.                 ;;
  222.             *)
  223.                 $chkconfig -f ypserv on
  224.                 $chkconfig -f yp on
  225.                 ;;
  226.             esac
  227.         fi
  228.     fi
  229.  
  230.     echo ""
  231.     echo "\
  232. At this point, make sure that /etc/passwd, /etc/hosts, /etc/networks,"
  233.     echo "\
  234. /etc/group, /etc/protocols, /etc/services, /etc/rpc and /etc/netgroup have"
  235.     echo "\
  236. been edited so that when the NIS is activated, the data bases you"
  237.     echo "\
  238. have just created will be used."
  239.  
  240.     exit 0
  241. else
  242.  
  243.     rm -f $yproot_dir/*.time
  244.     touch $yproot_dir/ypmake.log.old # prevent warning first time ypmake runs
  245.  
  246.     while [ $got_host_list = F ]; do
  247.         echo $host >$hf
  248.         if [ "$setup" != "yes" ]; then
  249.             echo ""
  250.             echo "\
  251. At this point, we have to construct a list of the hosts which will run NIS"
  252.             echo "\
  253. servers.  $host is in the list of NIS server hosts."
  254.             echo "\
  255. Please continue to add the names for the other hosts, one per line."
  256.             echo "\
  257. When you are done with the list, type a <control-D>."
  258.             echo "    next host to add:  $host"
  259.             echo "    next host to add:  \c"
  260.  
  261.             while read h
  262.             do
  263.                 echo "    next host to add:  \c"
  264.                 if test -z "$h"; then
  265.                     :
  266.                 else
  267.                     echo $h >>$hf
  268.                 fi
  269.             done
  270.  
  271.             echo ""
  272.             echo "The current list of NIS servers looks like this:"
  273.             echo ""
  274.  
  275.             cat $hf
  276.             echo ""
  277.             echo "Is this correct?  [y/n: y]  \c"
  278.             read hlist_ok
  279.  
  280.             case $hlist_ok in
  281.             n* | N*)
  282.                 got_host_list=F
  283.                 echo "Let's try the whole thing again...";;
  284.             *)    got_host_list=T;;
  285.             esac
  286.         else 
  287.             got_host_list=T
  288.         fi
  289.     done
  290.  
  291.     echo "\
  292. There will be no further questions. The remainder of the procedure should take"
  293.     echo "5 to 10 minutes."
  294.  
  295.     mv $hf $yproot_dir/ypservers
  296.     if [ $?  -ne 0 ]
  297.     then
  298.         echo "\
  299. Couldn't create NIS $yproot_dir/ypservers file." 1>&2 
  300.         errors_in_setup=T
  301.  
  302.         if [ $exit_on_error = T ]
  303.         then
  304.             exit 1
  305.         fi
  306.     fi
  307.  
  308.     in_pwd=`pwd`
  309.     cd $yproot_dir
  310.     echo "Building NIS databases:"
  311.     $YPM NOPUSH=1 
  312.  
  313.     if [ $?  -ne 0 ]
  314.     then
  315.         echo "\
  316. Error running NIS makefile." 1>&2
  317.         errors_in_setup=T
  318.         
  319.         if [ $exit_on_error = T ]
  320.         then
  321.             exit 1
  322.         fi
  323.     fi
  324.  
  325.     cd $in_pwd
  326.     echo ""
  327.  
  328.     if [ $errors_in_setup = T ]
  329.     then
  330.         echo "$host has been set up as the NIS master server with errors." 1>&2
  331.         echo "Please remember to figure out what went wrong, and fix it." 1>&2
  332.     else
  333.         echo "$host has been set up as the NIS master server without any errors."
  334.  
  335.         if $chkconfig ypmaster; then :
  336.         else
  337.             echo ""
  338.             echo "Start NIS master daemons during system startup? [y/n: y] \c"
  339.             read startyp
  340.             case $startyp in
  341.             n* | N*)
  342.                 ;;
  343.             *)
  344.                 $chkconfig -f ypmaster on
  345.                 $chkconfig -f ypserv on
  346.                 $chkconfig -f yp on
  347.                 ;;
  348.             esac
  349.         fi
  350.     fi
  351.  
  352.     echo ""
  353.     echo "\
  354. If there are running slave NIS servers, run yppush now for any data bases"
  355.     echo "\
  356. which have been changed.  If there are no running slaves, run ypinit on"
  357.     echo "\
  358. those hosts which are to be slave servers."
  359.  
  360. fi
  361.